<template>
  <el-dialog :append-to-body="true" :visible.sync="dialog" :title="isAdd ? '新增' : '编辑'" width="530px">
    <el-form ref="form" :model="form" :rules="rules" size="small" label-width="100px">
      <!-- <el-form-item label="巡检类型id" >
        <el-input v-model="form.typeid" style="width: 370px;"/>
      </el-form-item> -->
      <!-- <el-form-item label="巡检类型名称" >
        <el-input v-model="form.typename" style="width: 370px;"/>
      </el-form-item> -->

     <el-form-item label="巡检类型名称" >
       <el-select v-model="form.typeid" placeholder="巡检类型" v-on:change="indexSelect($event)">
           <el-option v-for="(item,index) in inspectionType" :key="index" :label="item.name" :value="item.value">
           </el-option>
      </el-select>
      </el-form-item>


      <!-- <el-form-item label="巡检对象id" >
        <el-input v-model="form.objid" style="width: 370px;"/>
      </el-form-item> -->
      <el-form-item label="巡检对象" >
       <el-select v-model="form.objid" placeholder="巡检对象" >
           <el-option v-for="(itemobject,index) in inspectionObject" :key="index" :label="itemobject.label" :value="itemobject.id">
           </el-option>
      </el-select>
      </el-form-item>

<!-- 

      <el-form-item label="巡检对象" >
        <el-input v-model="form.objname" style="width: 370px;"/>
      </el-form-item> -->
      <el-form-item label="巡检项目" >
        <el-input v-model="form.itemname" style="width: 370px;"/>
      </el-form-item>
      <el-form-item label="巡检内容" >
        <el-input v-model="form.itemcontent" style="width: 370px;"/>
      </el-form-item>
      <el-form-item label="合格标准" >
        <el-input v-model="form.itemstandard" style="width: 370px;"/>
      </el-form-item>
      <el-form-item label="备注" >
        <el-input v-model="form.remark" style="width: 370px;"/>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="text" @click="cancel">取消</el-button>
      <el-button :loading="loading" type="primary" @click="doSubmit">确认</el-button>
    </div>
  </el-dialog>
</template>

<script>
import request from '@/utils/request'
import { add, edit} from '@/api/inspectionItem'
export default {
  props: {
    isAdd: {
      type: Boolean,
      required: true
    },
    sup_this: {
      type: Object,
      default: null
    }
  },
  data() {
    return {
      loading: false, dialog: false,isShowSelect: false,
      form: {
        // id: '',
        // typeid: '',
        typename: '',
        // objid: '',
        objname: '',
        itemname: '',
        itemcontent: '',
        itemstandard: '',
        remark: ''
      },
      rules: {
      },
      dictId:0,
      inspectionObject:[],
      inspectionType:[{
            value:1,
            name:'设备类巡检'
        },{
            value:2,
            name:'设施类巡检',
        },{
            value:3,
            name:'管理类巡检'
        }],
    
    }
  },

  methods: {
    byNameinfo(name) {
      this.byName(name).then(res => {
        this.bydictId(res.id).then(resobject => {
           this.inspectionObject=resobject;
        //根据id再进行全部查询
      })
        //根据id再进行全部查询
      })
    },
    cancel() {
      this.resetForm()
    },
    doSubmit() {
      this.loading = true
      if (this.isAdd) {
        this.doAdd()
      } else this.doEdit()
    },
    doAdd() {
      add(this.form).then(res => {
        this.resetForm()
        this.$notify({
          title: '添加成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.$parent.$parent.init()
      }).catch(err => {
        this.loading = false
        console.log(err.response.data.message)
      })
    },
    doEdit() {
      edit(this.form).then(res => {
        this.resetForm()
        this.$notify({
          title: '修改成功',
          type: 'success',
          duration: 2500
        })
        this.loading = false
        this.sup_this.init()
      }).catch(err => {
        this.loading = false
        console.log(err.response.data.message)
      })
    },

    indexSelect(event){
       debugger;
    　　//console.log(event.target.label);//在这里可以正确输出每个下拉框对应的下标值，当然输出值都是可以的
        if(event==1){
            this.byNameinfo("设备类巡检");
        }else if(event==2){
            this.byNameinfo("设施类巡检");
        }else if(event==3){
            this.byNameinfo("管理类巡检");
        }
       
        //设备类巡检对象
    },
    // 获取id值
    byName(name) {
      return request({
        url: 'api/dict/byName/' + name,
        method: 'get'
      })
    },
        // 获取id值
    bydictId(id) {
      return request({
        url: 'api/dictDetail/findByDeptId/' + id,
        method: 'get'
      })
    },
    resetForm() {
      this.dialog = false
      this.$refs['form'].resetFields()
      this.form = {
        id: '',
        typeid: '',
        typename: '',
        objid: '',
        objname: '',
        itemname: '',
        itemcontent: '',
        itemstandard: '',
        remark: ''
      }
    }
  }
};
</script>

<style scoped>

</style>
